import execjs
import requests
headers = {
    "Accept": "application/json, text/plain, */*",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Connection": "keep-alive",
    "Content-Type": "application/x-www-form-urlencoded",
    "Origin": "https://fanyi.youdao.com",
    "Referer": "https://fanyi.youdao.com/",
    "Sec-Fetch-Dest": "empty",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Site": "same-site",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36",
    "sec-ch-ua": "\"Chromium\";v=\"136\", \"Google Chrome\";v=\"136\", \"Not.A/Brand\";v=\"99\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\""
}
cookies = {
    "OUTFOX_SEARCH_USER_ID": "-2053209549@112.193.83.244",
    "OUTFOX_SEARCH_USER_ID_NCOO": "494456421.74573296",
    "_uetsid": "81e5a920390511f091132d033519cf23",
    "_uetvid": "5d13a5d0233811f08b78033a5bcab5a6",
    "DICT_DOCTRANS_SESSION_ID": "ODkwMDU3YTUtNmFiYS00N2M5LWJiZWMtYWM1ZTFhOGE2ZGQ5"
}

js_code = execjs.compile(open('youdao.js', 'r', encoding='utf-8').read())
time_now,sign=js_code.call('get_time_sign')
a=input("请输入中文翻译成英文：")

url = "https://dict.youdao.com/webtranslate"
data = {
    "i": a,
    "from": "zh-CHS",
    "to": "en",
    "useTerm": "false",
    "domain": "0",
    "dictResult": "true",
    "keyid": "webfanyi",
    "sign": sign,
    "client": "fanyideskweb",
    "product": "webfanyi",
    "appVersion": "1.0.0",
    "vendor": "web",
    "pointParam": "client,mysticTime,product",
    "mysticTime": time_now,
    "keyfrom": "fanyi.web",
    "mid": "1",
    "screen": "1",
    "model": "1",
    "network": "wifi",
    "abtest": "0",
    "yduuid": "abcdefg"
}
response = requests.post(url, headers=headers, cookies=cookies, data=data)
r = js_code.call('r', response.text)
print(r)
